﻿using RabbitMQ.Client;
using System.Text;

namespace RabbitMQDemo.Producer
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Start the producer");

            var connFactory = new ConnectionFactory
            {
                HostName = "106.53.127.56",
                Port = 5672,
                UserName = "rabbitmqdemo",
                Password = "rabbitmqdemo@test",
                VirtualHost = "rabbitmqdemo"
            };
            using (var conn = connFactory.CreateConnection())
            {
                using (var channel = conn.CreateModel())
                {
                    var exchangeName = "topics_exchange";
                    channel.ExchangeDeclare(exchange: exchangeName, type: "topic");
                    while (true)
                    {
                        Console.WriteLine("消息RoutingKey:");
                        var routingKey = Console.ReadLine();

                        Console.WriteLine("消息内容(exit退出):");
                        var message = Console.ReadLine();
                        if (message.Trim().ToLower() == "exit")
                        {
                            break;
                        }

                        var body = Encoding.UTF8.GetBytes(message);
                        channel.BasicPublish(exchange: exchangeName, routingKey: routingKey, basicProperties: null, body: body);
                        Console.WriteLine("消息内容发送完毕:" + message);
                    }
                }
            }
        }
    }
}